<template>
  <div class="wrapper">
    aabr <br />
    nn
    <input type="button" @click="iot()" value="startConnect" />
    <input type="button" @click="publish()" value="publish" />
  </div>
</template>
<script>
  const path = require('path');
  const Mqtt = require('mqtt');
  const RandomString = require('randomstring');
  export default {
    data() {
      return {
        fs: require('fs'),
        date: new Date().Format('yyyy-MM-dd hh:mm:ss'),
        start: '',
        end: '',
        num: 100,
        weekRange: [],
        show: false,
        TEXT: '',
        mqttClient: '',
        mqttParam: {}
      };
    },
    provide: {
      for: 'demo'
    },
    ttt: { xx: 1 },
    computed: {
      ctest: () => {
        return 'ctest**';
      },
      dizzy: {
        set: function(v) {
          return (this.num = v * 3);
        },
        get: function() {
          return 'get' + this.num;
        }
      }
    },
    methods: {
      showOS() {
        var os = require('os');
        var osInfo;
        osInfo += '操作系统=' + os.platform();
        osInfo += '系统版本=' + os.release();
        osInfo += '系统名称=' + os.type();
        osInfo += 'CPU架构=' + os.arch();
        console.log(osInfo, os.cpus().length);
      },
      preweek() {
        var _this = this;
        _this.date = new Date(Date.parse(_this.date) - 24 * 60 * 60 * 1000 * 7);
        _this.calc();
      },
      nexweek() {
        var _this = this;
        _this.date = new Date(Date.parse(_this.date) + 24 * 60 * 60 * 1000 * 7);
        _this.calc();
      },
      calc() {
        var _this = this;
        var stvarible = new Date(_this.date).getDay() - 1;
        if (stvarible < 0) {
          stvarible = 7 + stvarible;
        }
        _this.start = new Date(Date.parse(_this.date) - stvarible * 24 * 60 * 60 * 1000).Format(
          'yyyy-MM-dd hh:mm:ss'
        );
        var edvarible = 7 - new Date(_this.date).getDay();
        if (edvarible == 7) {
          edvarible = 0;
        }
        _this.end = new Date(Date.parse(_this.date) + edvarible * 24 * 60 * 60 * 1000).Format(
          'yyyy-MM-dd hh:mm:ss'
        );
      },
      onClickLeft() {},
      onClickRight() {},
      fstest() {
        var _this = this;
        _this.getLessVariables(_this.resolve('@style/globalLessVal.js'), _this);
      },
      resolve(dir) {
        return path.join(__dirname, dir);
      },
      getLessVariables() {
        //var fs = require('fs');
        //console.log(require('domain'));
        return;
        // this.$nextTick(()=>{
        //   console.log(fs, 'nexttick',require('crypto').createHmac('sha256', '111').update('I love cupcakes').digest('hex'));
        // });
        // console.log(require('fs'), 'value of fs', this.fs);
        // let themeContent = this.fs.readFileSync(file, 'utf-8');
        // let variables = {};
        // themeContent.split('\n').forEach(function(item) {
        //   if (item.indexOf('//') > -1 || item.indexOf('/*') > -1) {
        //     return;
        //   }
        //   let _pair = item.split(':');
        //   if (_pair.length < 2) return;
        //   let key = _pair[0].replace('\r', '').replace('@', '');
        //   if (!key) return;
        //   variables[key] = _pair[1]
        //     .replace(';', '')
        //     .replace('\r', '')
        //     .replace(/^\s+|\s+$/g, '');
        // });
        // return variables;
      },
      iot() {
        var _this = this;
        _this.mqttParam = {
          server: 'ssl://81891x2.mqtt.iot.gz.baidubce.com:1884',
          options: {
            username: '81891x2/client1',
            password: 'MjODK6ZHBy8S7rGr',
            clientId: 'DeviceId-mhzoj5p1xx' + RandomString.generate()
          },
          topic: 'toTrang'
        };
        // _this.mqttParam = {
        //   server: 'tcp://81891x2.mqtt.iot.gz.baidubce.com:1883',
        //   options: {
        //     username: '81891x2/client1',
        //     password: 'MjODK6ZHBy8S7rGr',
        //     clientId: 'DeviceId-mhzoj5p1xx' + RandomString.generate()
        //   },
        //   topic: 'toTrang'
        // };
        //console.log(Mqtt,RandomString,_this.mqttParam.options);
        _this.mqttClient = Mqtt.connect(_this.mqttParam.server, _this.mqttParam.options);
        console.log('Connecting to broker: ' + _this.mqttParam.server);

        _this.mqttClient.on('error', function(error) {
          console.error(error);
        });

        _this.mqttClient.on('message', function(topic, data) {
          console.log('MQTT message received: ' + data);
          if (data.toString() === 'exit') process.exit();
        });

        _this.mqttClient.on('connect', function() {
          console.log('Connected. Client id is: ' + _this.mqttParam.options.clientId);

          _this.mqttClient.subscribe(_this.mqttParam.topic);
          console.log('Subscribed to topic: ' + _this.mqttParam.topic);

          _this.mqttClient.publish(_this.mqttParam.topic, 'Message from Baidu IoT demo');
          console.log('MQTT message published.');
        });
      },
      publish() {
        var _this = this;
        _this.mqttClient.publish(_this.mqttParam.topic, 'test');
      }
    },
    mounted() {
      var _this = this;
      _this.calc();
      //_this.showOS();
    }
  };
</script>
<style scoped lang="less">
  .wrapper {
    background-image: url('~@assets/logo.png');
  }
</style>
